BEGIN { push(@INC,'./build/perl');}
use Scopes;

use Devel::StackTrace;
sub test{
   if ($_[0]){
      print $_[1]."... Ok\n";
   } else {
      die "Error during ".$_[1]."\n";
   }
}

$SIG{__DIE__} = sub {
        my $error = shift;
        my $trace = Devel::StackTrace->new;
        print "==========================================================================\nError: $error\n",
              "Stack Trace:\n",
              $trace->as_string;
};

$|=1;
print "generating an large number of random arrays, this should not segfault nor go out of memory... ";
for (my $i=0;$i<10000;$i++){
   my $a=new Scopes::ArrayS(int(rand(200)));
   $a->random(10);
#   $a->print();
   my $b=$a->which();
   die "which/lenght error" unless $b->size()==$a->nz();
#   $b->print();
   my $c=new Scopes::ArrayS($a->size(),1);
   $a->add($c);
   die "add error" unless $a->size()==$a->nz();
}
print "ok.\n";

$a=new Scopes::ArrayU8([1,1,1,0,0,0,1,1]);
$b=new Scopes::ArrayU8([1,1,0,1,1,0,1,1]);

$c=$a;
$c=0; # should not segfault;

$c=$a;
$c->put(2,3);
test($a->isEq(new Scopes::ArrayU8([1,1,3,0,0,0,1,1])),"reference is modified");


$c=$a->copy();
$c->OR($b);
test($c->isSetEq(new Scopes::ArrayU8([1,1,1,1,1,0,1,1])),"boolean OR");
$c=$a->copy();
$c->subSet($b);
test($c->isSetEq(new Scopes::ArrayU8([0,0,1,0,0,0,0,0])),"boolean sub");
$c=$a->copy();
$c->XOR($b);
test($c->isSetEq(new Scopes::ArrayU8([0,0,1,1,1,0,0,0])),"boolean XOR");

sleep(1); # wait a bit before starting perl garbage collection on exit

